package com.example.wangzh.developkit.control;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.utils.TransformUtils;
import com.example.wangzh.developkit.utils.ViewUtils;
import com.example.wangzh.developkit.widget.AmountSelector;
import com.example.wangzh.developkit.widget.MeshNumberSelector;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by USER on 2017/1/16.
 */

public class LockGoodsPopupManager {
	Activity activity;
	Button btn_lock_goods;
	PopupWindow popupWindow;
	AmountSelector amountSelector;
	MeshNumberSelector meshNumberSelector;
	LinearLayout lnl_show_mesh;
	double stockAmount = 500;
	double price = 100;
	int minMesh, maxMesh;
	boolean needMesh;
	@BindView(R.id.txt_cost)
	TextView txt_cost;
	@BindView(R.id.txt_mesh_number)
	TextView txt_mesh_number;
	@BindView(R.id.rll_mesh)
	RelativeLayout rll_mesh;
	@BindView(R.id.lnl_cost)
	LinearLayout lnl_cost;
	@BindView(R.id.txt_price)
	TextView txt_price;
	@BindView(R.id.lnl_price)
	LinearLayout lnl_price;
	@BindView(R.id.txt_amount)
	TextView txt_amount;
	OnGoodsLockedListener onGoodsLockedListener;

	public interface OnGoodsLockedListener {
		void onGoodsLocked(double amount);
	}


	public void setOnGoodsLockedListener(OnGoodsLockedListener onGoodsLockedListener) {
		this.onGoodsLockedListener = onGoodsLockedListener;
	}

	public void setStockAmount(double stockAmount) {
		this.stockAmount = stockAmount;
		if (amountSelector != null) {
			amountSelector.setStockAmount(stockAmount);
		}
	}

	public int getMinMesh() {
		return minMesh;
	}

	public void setMinMesh(int minMesh) {
		this.minMesh = minMesh;
		if (meshNumberSelector != null) {
			meshNumberSelector.setMinMesh(minMesh);
			refreshMesh( );
		}
	}


	public int getMaxMesh() {
		return maxMesh;
	}

	public void setMaxMesh(int maxMesh) {
		this.maxMesh = maxMesh;
		if (meshNumberSelector != null) {
			meshNumberSelector.setMaxMesh(maxMesh);
			refreshMesh( );
		}
	}

	public boolean isNeedMesh() {
		return needMesh;
	}

	public void setNeedMesh(boolean needMesh) {
		this.needMesh = needMesh;
		if (rll_mesh != null) {
			ViewUtils.visibleOrGone(rll_mesh, needMesh);
		}
	}

	public void setPrice(double price) {
		this.price = price;
		if (txt_price != null) {
			txt_price.setText(TransformUtils.transformDouble(price));
		}
	}

	public LockGoodsPopupManager(Activity activity) {
		this.activity = activity;
	}

	protected boolean needHeavy() {
		return true;
	}

	protected String buttontext() {
		return "立即锁货";
	}

	public PopupWindow getPopupWindow() {
		View contentView = LayoutInflater.from(activity).inflate(R.layout.popup_lock_goods, null);
		ButterKnife.bind(this, contentView);
		btn_lock_goods = (Button) contentView.findViewById(R.id.btn_lock_goods);
		amountSelector = (AmountSelector) contentView.findViewById(R.id.amountSelector);
		meshNumberSelector = (MeshNumberSelector) contentView.findViewById(R.id.meshNumberSelector);
		lnl_show_mesh = (LinearLayout) contentView.findViewById(R.id.lnl_show_mesh);
		amountSelector.setStockAmount(stockAmount);
		meshNumberSelector.setMinMesh(minMesh);
		meshNumberSelector.setMaxMesh(maxMesh);
		txt_mesh_number.setText(minMesh + "");
		txt_price.setText(TransformUtils.transformDouble(price));
		txt_amount.setText(TransformUtils.transformDouble(amountSelector.getRequireAmount()));
		refreshMesh( );
		ViewUtils.visibleOrGone(lnl_price, needHeavy( ));
		ViewUtils.visibleOrGone(rll_mesh, needMesh);
		ViewUtils.visibleOrGone(amountSelector, needHeavy( ));
		ViewUtils.visibleOrGone(lnl_cost, needHeavy( ));
		txt_cost.setText(TransformUtils.transformDouble(getAmount( ) * price));
		amountSelector.setOnAmountChangeListener(new AmountSelector.OnAmountChangeListener( ) {
			@Override
			public void OnAmountChange(double amount) {
				txt_cost.setText("¥" + TransformUtils.transformDouble(amount * price));
				txt_amount.setText(TransformUtils.transformDouble(amount));
			}
		});
		btn_lock_goods.setOnClickListener(new View.OnClickListener( ) {
			@Override
			public void onClick(View v) {

				if (meshNumberSelector.getVisibility( ) != View.GONE) {
					if (meshNumberSelector.getRequireAmount( ) < minMesh || meshNumberSelector.getRequireAmount( ) > maxMesh) {
						Toast.makeText(activity, "目数错误", Toast.LENGTH_SHORT).show( );
						return;
					}
				}

				if (needHeavy( )) {
					if (amountSelector.getRequireAmount( ) <= 0 || amountSelector.getRequireAmount( ) > stockAmount) {
						Toast.makeText(activity, "数量错误", Toast.LENGTH_SHORT).show( );
						return;
					}
				}
				popupWindow.dismiss( );
				if (onGoodsLockedListener != null) {

					onGoodsLockedListener.onGoodsLocked(amountSelector.getRequireAmount( ));
				}
			}
		});
		btn_lock_goods.setText(buttontext( ));
		popupWindow = new PopupWindow(contentView,
				LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);

		popupWindow.setTouchable(true);

		popupWindow.setTouchInterceptor(new View.OnTouchListener( ) {

			@Override
			public boolean onTouch(View v, MotionEvent event) {


				return false;
				// 这里如果返回true的话，touch事件将被拦截
				// 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
			}
		});

		// 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
		popupWindow.setBackgroundDrawable(new BitmapDrawable( ));
		popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener( ) {
			@Override
			public void onDismiss() {
				setActivityBackgroundAlpha(1.0f);
			}
		});
		popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
		popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		// 我觉得这里是API的一个bug
		return popupWindow;
	}

	private void refreshMesh() {
		if (maxMesh == minMesh) {
			ViewUtils.switchView(lnl_show_mesh, meshNumberSelector);
			meshNumberSelector.setRequireAmount(minMesh);
		} else {
			ViewUtils.switchView(meshNumberSelector, lnl_show_mesh);
		}
	}

	public void showAtLocation(View parent, int gravity, int x, int y) {
		setActivityBackgroundAlpha(0.5f);
		getPopupWindow( ).showAtLocation(parent, gravity, x, y);
	}

	/**
	 * 设置添加屏幕的背景透明度
	 *
	 * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
	 */
	public void setActivityBackgroundAlpha(float bgAlpha) {
		WindowManager.LayoutParams lp = activity.getWindow( )
				.getAttributes( );
		lp.alpha = bgAlpha;
		activity.getWindow( ).setAttributes(lp);
	}

	public double getAmount() {
		return amountSelector.getRequireAmount( );
	}

	public int getMeshNumber() {
		return (int) meshNumberSelector.getRequireAmount( );
	}
}
